<?php
namespace Attach\Controller;
use Common\Controller\AddonController;

class IndexController extends AddonController {
    public function index() {
        $this->IS_POST(function (){
            $model = D('Common/File');
            $keyword = I('keyword');
            $keywords = '%'.str_replace(' ', '%', I('keyword')).'%';
            if($keyword) {
                $map['name|savename|from|savepath|mime'] = ['like', $keywords];
            }
            $list = $model->where($map)->findPage();
            foreach ($list['data'] as &$row) {
                if(stripos($row['mime'], 'image')!==false) {
                    $row['is_img'] = true;
                }
            }
            $this->ajaxReturn($list);
        });
        $this->_web('title','附件管理');
        $this->_web('panel_title','附件管理');
        $listData = array(
            'url' => U(''),
            'toolbar' => '#barDemo',
            'toolbarTpl' => 'toolbar',
            'page' => true,
        );
        $listData['cols'][] = ['type' => 'checkbox', 'fixed' => 'left'];
        $listData['cols'][] = array('field' => 'id', 'title' => 'ID', 'sort' => true);
        $listData['cols'][] = array('field' => 'name', 'title' => '文件名', 'width' => '15%');
        $listData['cols'][] = array('field' => 'size_text', 'title' => '文件大小', 'width' => '8%');
        $listData['cols'][] = array('field' => 'mime', 'title' => '文件类型', 'width' => '8%');
        $listData['cols'][] = array('field' => 'rootpath', 'title' => '保存根路径', 'width' => '8%');
        $listData['cols'][] = array('field' => 'savepath', 'title' => '保存路径', 'width' => '8%');
        $listData['cols'][] = array('field' => 'savename', 'title' => '保存文件名', 'width' => '8%');
        $listData['cols'][] = array('field' => 'from', 'title' => '文件来源', 'width' => '8%');
        $listData['cols'][] = array('field' => 'username', 'title' => '上传用户', 'width' => '8%');
        $listData['cols'][] = array('field' => 'create_time_text', 'title' => '上传时间', 'width' => '10%');
        $listData['cols'][] = array('fixed' => 'right', 'width' => '220', 'title' => '操作', 'toolbar' => '#rowBar');
        $this->displayList($listData);
    }
    public function del() {
        $ids = I('id');
        if(is_string($ids)) {
            $ids = explode(',', $ids);
        }
        $model = D('Common/File');
        $res = $model->where(['id'=>['in',$ids]])->delete();
        if($res) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }
    public function editName($id) {
        $model = D('Common/File');
        $this->IS_POST(function () use($model) {
            $data = $model->field('id,name')->create();
            if(!$data){
                $this->error($model->getError());
            }else if($model->save($data)!==false) {
                $this->success('编辑成功');
            }
            $this->error('编辑失败');
        });
        $name = $model->getFieldById($id, 'name');
        $this->assign('id', $id);
        $this->assign('name', $name);
        $this->display();
    }
    public function viewImg($id) {
        $map = ['id'=>$id];
        $model = D('Common/File');
        $row = $model->where($map)->find();
        if(!$row) $this->error('文件不存在');
        $this->assign('id', $id);
        $this->display();
    }
}
